/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.sensor.greenseeker;

import org.junit.Before;
import nl.cloudfarming.client.model.SensorMetadata;
import nl.cloudfarming.client.model.SensorDataFile;
import org.junit.After;
import java.io.File;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Gerben Feenstra
 */
public class GreenseekerProcessorProviderIT {

    static final File NORMAL_SENSORFILE = new File("./src/test/resources/M20100905_NORMAL_TEST.dbf");
    static final File NORMAL_SENSORFILE_FAIL = new File("./src/test/resources/M20100905_NORMAL_TEST_ERROR.dbf");
    static final File BORNE_SENSORFILE = new File("./src/test/resources/M20100531_BORNE_TEST.dbf");

    static final File SENSORFILE_EXTENSION_FAIL = new File("./src/test/resources/M20100905_nonexistent.ext");

    static final GreenseekerProcessorProvider greenseekerProcessorProvider = new GreenseekerProcessorProvider();


    @After
    public void cleantable(){
        GreenseekerServiceImpl service = new GreenseekerServiceImpl();
        for(GreenseekerSensorData sensorData : service.findAllGreenseekerSensorData()){
            service.delete(sensorData);
        }
        for(SensorDataFile dataFile : service.findAllSensorDataFiles()){
            service.removeSensorDataFile(dataFile);
        }
        for(SensorMetadata metadata : service.findAllSensorMetadata()){
            service.remove(metadata);
        }
        for (GreenseekerSensorData g : service.findAllGreenseekerSensorData()) {
            service.delete(g);
        }
    }
    
    @Before
    public void setUp(){
         cleantable();
    }

    @Test
    public void checkExistenceOfTestFiles(){
        assertTrue(NORMAL_SENSORFILE.exists());
        assertTrue(NORMAL_SENSORFILE_FAIL.exists());
        assertTrue(BORNE_SENSORFILE.exists());
    }

    @Test
    public void isSupportedSuccessTest(){
        assertTrue(greenseekerProcessorProvider.isSupported(NORMAL_SENSORFILE));
        assertTrue(greenseekerProcessorProvider.isSupported(BORNE_SENSORFILE));

    }

    @Test
    public void isSupportedFailedExtensionTest(){
        assertFalse(greenseekerProcessorProvider.isSupported(SENSORFILE_EXTENSION_FAIL));
    }

    @Test
    public void isSupportedFailedHeaderTest(){        
        assertFalse(greenseekerProcessorProvider.isSupported(NORMAL_SENSORFILE_FAIL));
    }

    @Test
    public void isValidNormalSuccesTest(){
        assertTrue(greenseekerProcessorProvider.isValid(NORMAL_SENSORFILE));
    }

    @Test
    public void isValidBorneSuccesTest(){
        assertTrue(greenseekerProcessorProvider.isValid(BORNE_SENSORFILE));
    }

//    @Test
    public void isValidNormalFailTest(){
        // Process file, will validate file in the process
        GreenseekerFileProcessorNormal gfpNormal = new GreenseekerFileProcessorNormal(NORMAL_SENSORFILE);
        gfpNormal.process();

        // Try validating same file again. Should fail because files has already been imported.
        assertFalse(greenseekerProcessorProvider.isValid(NORMAL_SENSORFILE));


    }

//    @Test
    public void isValidBorneFailTest(){
        // Process file, will validate file in the process
        GreenseekerFileProcessorNormal gfpBorne = new GreenseekerFileProcessorNormal(BORNE_SENSORFILE);
        gfpBorne.process();

        // Try validating same file again. Should fail because file has already been imported.
        assertFalse(greenseekerProcessorProvider.isValid(BORNE_SENSORFILE));
    }
}
